package cn.com.hfuu.service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import cn.com.hfuu.ExcelUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.UserException;
import cn.com.hfuu.dao.userMapper;
import cn.com.hfuu.module.user;

@Service("userService")
public class UserService {

	@Autowired
	private userMapper usermapper;

	/**
	 * 验证用户
	 * 
	 * @return
	 */
	public user exitUser(String username, String password) {
		user u = usermapper.selectByUsername(username);
		// System.out.println(u.toString());
		if (u == null) {
			return null;
		}
		if (u.getUserPassword().equals(password)) {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			u.setLastVisit(sdf.format(date));
			userTimeUpdate(u);
			return u;
		} else {
			return null;
		}
	}

	public user exitUsername(String username) {
		user u = usermapper.selectByUsername(username);
		return u;
	}

	public user exitUsernameedit(String username, String id) {
		user u = usermapper.selectByUsernameId(username, id);
		return u;
	}

	public int userTimeUpdate(user u) {
		return usermapper.updateByPrimaryKeySelective(u);
	}

	public List<user> userListpage(int start, int limit) {

		// TODO Auto-generated method stub
		List<user> userlist = usermapper.selectAllpage(start, limit);
		return userlist;
	}

	public List<user> userList() {

		// TODO Auto-generated method stub
		List<user> userlist = usermapper.selectAll();
		return userlist;
	}

	public List<user> searchUserpage(String username, String userId,
			int userstate, int current, int size) {
		List<user> userlist = usermapper.searchUserpage(username, userId,
				userstate, current, size);
		return userlist;

	}

	public List<user> searchUser(String username, String userId, int userstate) {
		List<user> userlist = usermapper
				.searchUser(username, userId, userstate);
		return userlist;

	}

	public int adduser(user u) {
		// TODO Auto-generated method stub
		return usermapper.insertSelective(u);
	}

	public user selectuserbyid(String id) {
		return usermapper.selectByPrimaryKey(id);
	}

	public int updateuser(user u) {
		return usermapper.updateByPrimaryKeySelective(u);
	}

	public int deleteuser(String[] userid) {
		return usermapper.deleteByPrimaryKeyusers(userid);

	}

	public List<user> selectyg(String id) {
		// TODO Auto-generated method stub
		return usermapper.selectyg(id);
	}

	public List<user> selectall() {
		// TODO Auto-generated method stub
		return usermapper.selectAll();
	}

	@Transactional
	public String upload(InputStream in, MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		List<List<Object>> listob = ExcelUtil.getBankListByExcel(in,
				file.getOriginalFilename());
		UUIdUtil idu=new UUIdUtil();
		for (int i=0;i<listob.size();i++) {
			List<Object> ob = listob.get(i);
 
			String state=String.valueOf(ob.get(1)).trim();
			String username=String.valueOf(ob.get(0)).trim();
			if(usermapper.selectByUsername(username)!=null){
				throw new RuntimeException("第"+(i+1)+"行用户名有重复");
			 
			}
			user u=new user();
			u.setUserName(username);
			u.setUserId(idu.getUuid());
			u.setUserPassword(String.valueOf(ob.get(2)));
			if(state.equals("正常")){
				u.setUserState(1);
			}else if(state.equals("锁定")){
				u.setUserState(1);
			}else{
				throw new RuntimeException( "第"+(i+1)+"行用户状态不正确");
			}
			System.out.println(u.toString());
			usermapper.insertSelective(u);
		}
		return "成功导入";
	}
}
